package com.juli.v2xdemo.presenter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.cz.basetool.ui_work.thread.MainHandler;
import com.example.tcpmaster.callback.AbsTcpUICallBack;


/**
 * Created by XinYi on 2018/11/2.
 */

public class TcpUICallBackImpl extends AbsTcpUICallBack {

    private TextView tvNetState;

    public TcpUICallBackImpl(Context context, View viewGroup) {
        super(context, viewGroup);
    }

    @Override
    public void release() {

    }

    @Override
    public void onConnectted() {
        setTxtOnUIThread("已连接");
    }

    @Override
    public void onConnectFailed(int code, String message) {
        setTxtOnUIThread(message);
    }

    @Override
    public void onReceive(String s) {

    }

    @Override
    public void onDisconnect() {
        setTxtOnUIThread("连接已经断开");
    }

    @Override
    public void onConnecting() {
        setTxtOnUIThread("连接中...");
    }

    @Override
    public void onException(Exception e) {
        e.printStackTrace();
        setTxtOnUIThread("连接异常" + e.getMessage());
    }

    @Override
    public void onRequestTimeOut() {

    }

    @Override
    public void onReconnect() {
        setTxtOnUIThread("正在重新连接");
    }

    @Override
    public void onStartConnect() {
        setTxtOnUIThread("开始连接");
    }

    @Override
    protected void init(Context context, View viewGroup) {
        tvNetState = (TextView) viewGroup;
    }

    private void setTxtOnUIThread(final String txt){
        MainHandler.getInstance().post(new Runnable() {
            @Override
            public void run() {
                tvNetState.setText(txt);
            }
        });
    }
}
